package logic.threadSafe;

/**线程安全计数类，封装对象,并用synchronized关键字保证多个线程写操作的安全性
 * 
 * @author munan
 *
 */
public final class Counter {
	private long num=0;
	
	public synchronized long getNum(){
		return num;
	}
	public synchronized long increment(){
		if(num==Long.MAX_VALUE)throw new IllegalStateException("counter overflow!");
		return ++num;
	}
	
	
}
